<template>
  <div class="message">
    <div class="search__filter">
      <SearchBox @search="search">
        <div class="common-search-item">
          <div class="label">来源:</div>
          <div class="input">
            <el-select v-model="searchFilter.type" placeholder="请选择" @change="search">
              <el-option v-for="item in froms" :key="item.value" :label="item.label" :value="item.value" />
            </el-select>
          </div>
        </div>
        <div class="common-search-item">
          <div class="label">状态:</div>
          <div class="input">
            <el-select v-model="searchFilter.status" placeholder="请选择" @change="search">
              <el-option v-for="item in status" :key="item.value" :label="item.label" :value="item.value" />
            </el-select>
          </div>
        </div>
        <div class="common-search-item">
          <div class="label">姓名:</div>
          <div class="input">
            <el-input v-model="searchFilter.name" placeholder="姓名" clearable @input="debounceSearch" />
          </div>
        </div>
        <div class="common-search-item">
          <div class="label">电话:</div>
          <div class="input">
            <el-input v-model="searchFilter.phone" placeholder="电话" clearable @input="debounceSearch" />
          </div>
        </div>
        <div class="common-search-item">
          <div class="label">邮箱:</div>
          <div class="input">
            <el-input v-model="searchFilter.email" placeholder="邮箱" clearable @input="debounceSearch" />
          </div>
        </div>
      </SearchBox>
    </div>
    <div class="list__wrapper">
      <el-table v-loading="loading" :data="tableData" element-loading-background="rgba(0, 0, 0, 0.8)">
        <el-table-column type="index" width="50" />
        <template v-if="searchFilter.type === 2">
          <el-table-column prop="company" label="公司名称" />
          <el-table-column prop="address" label="公司地址" />
          <el-table-column prop="product" label="行业产品" />
        </template>
        <el-table-column prop="name" label="姓名" />
        <el-table-column prop="phone" label="电话" />
        <el-table-column prop="message" label="留言" />
        <el-table-column prop="message" label="状态">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status == 0" size="medium" type="danger">未处理</el-tag>
            <el-tag v-else size="medium" type="success">已处理</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="remark" label="备注" />
        <el-table-column label="操作" width="180">
          <template slot-scope="scope">
            <el-button v-if="scope.row.status == 0" size="mini" type="primary"
              @click="handleData(scope.row)">处理</el-button>
            <el-button size="mini" type="danger" @click="delData(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <Pagination :page-size="pageSize" :current-page="page" :total-size="totalSize" @changePageSize="changePageSize"
        @changeCurrentPage="changeCurrentPage" />
    </div>
    <el-dialog title="处理" :visible.sync="dialogVisible" width="30%">
      <el-form class="el-form">
        <el-form-item label="备注:" prop="virtualSystemName" class="w100p">
          <el-input v-model="form.remark" type="textarea" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" :disabled="!form.remark" @click="editData">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import SearchBox from '@/components/SearchBox';
import axios from 'axios';
import { getToken } from '@/utils/auth';
import Pagination from '@/components/Pagination';
import { debounce } from '@/utils/index';
export default {
  name: 'Message',
  components: {
    SearchBox,
    Pagination
  },
  data() {
    return {
      form: {
        remark: ''
      },
      dialogVisible: false,
      froms: [
        {
          label: '解决方案',
          value: 1
        },
        {
          label: '联系我们',
          value: 2
        }
      ],
      status: [
        {
          label: '全部',
          value: ''
        },
        {
          label: '未处理',
          value: 0
        },
        {
          label: '已处理',
          value: 1
        }
      ],
      searchFilter: {
        name: '',
        phone: '',
        email: '',
        type: 1,
        status: ''
      },
      page: 1,
      pageSize: 10,
      tableData: null,
      totalSize: 0,
      loading: false,
      currData: null
    };
  },
  computed: {
    databaseName() {
      return getToken('databaseName');
    }
  },
  created() {
    this.getList();
  },
  methods: {
    handleData(data) {
      this.currData = data;
      this.dialogVisible = true;
    },
    debounceSearch: debounce(function () {
      this.search();
    }, 250),
    search() {
      this.page = 1;
      this.getList();
    },
    async getList() {
      const { searchFilter, page, pageSize } = this;
      const searchFilterCopy = JSON.parse(JSON.stringify(searchFilter));
      if (searchFilterCopy.status === '') {
        delete searchFilterCopy.status;
      }
      this.loading = true;
      const res = await axios({
        method: 'post',
        url: this.$webSerUrlPro + 'config/websiteMessage/queryPageList',
        headers: {
          databaseName: this.databaseName
        },
        data: {
          page,
          size: pageSize,
          entity: searchFilterCopy
        }
      });
      this.loading = false;
      this.totalSize = res.data.data.totalElements;
      this.tableData = res.data.data.content;
    },
    editData() {
      this.$confirm('是否确定此操作?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const { form, currData } = this;
          await axios({
            method: 'post',
            url: this.$webSerUrlPro + 'config/websiteMessage/edit',
            headers: {
              databaseName: this.databaseName
            },
            data: {
              websiteMessageId: currData.websiteMessageId,
              status: 1,
              remark: form.remark
            }
          });
          this.dialogVisible = false;
          this.form.remark = '';
          this.getList();
        })
        .catch(() => { });
    },
    delData(currData) {
      this.$confirm('是否确定此操作?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          await axios({
            method: 'get',
            url: this.$webSerUrlPro + 'config/websiteMessage/del',
            headers: {
              databaseName: this.databaseName
            },
            params: {
              websiteMessageId: currData.websiteMessageId
            }
          });
          this.dialogVisible = false;
          this.getList();
        })
        .catch(() => { });
    },
    changePageSize(pageSize) {
      this.pageSize = pageSize;
      this.getList();
    },
    changeCurrentPage(currentPage) {
      this.page = currentPage;
      this.getList();
    }
  }
};
</script>

<style lang="scss" scoped>
.message {
  overflow-y: auto;

  .search__filter {
    background: #00171c;
    flex-shrink: 0;
    padding: 10px;
    margin-bottom: 10px;
  }

  .pagination {
    text-align: right;
    position: static;
    margin-top: 10px;
  }

  .el-form-item {
    &:last-child {
      margin-bottom: 0;
    }
  }
}
</style>
